package emulator.shell.GUI.debugger;

import emulator.assembler.Assembler;
import emulator.assembler.DataDescription;
import emulator.assembler.DataDescriptionTable;
import emulator.assembler.Instruction;

public class CodeInfo {
	private DataDescriptionTable description_table = null;
	private Assembler assembler = null;
	private byte[] memory;

	public CodeInfo(byte[] memory, Assembler assembler)
	{
		this.memory = memory;
		this.assembler = assembler;
		this.description_table = new DataDescriptionTable(0, memory.length-1, 8);
	}
	
	public DataDescriptionTable getDescriptionTable() {
		return description_table;
	}

	public void setDescriptionTable(DataDescriptionTable description_table) {
		this.description_table = description_table;
	}

	public Assembler getAssembler() {
		return assembler;
	}

	public byte[] getMemory() {
		return memory;
	}

	/**
	 * @return
	 * @see emulator.assembler.DataDescriptionTable#size()
	 */
	public int size() {
		return description_table.size();
	}

	public String getJumpLabel(int address) {
		return assembler.getSymbols().getJumpLabel(address);
	}

	public String getLabel(int address) {
		return assembler.getSymbols().getLabelOrAddress(address);
	}
	
	public String getRemark(int address) {
		return assembler.getSymbols().getRemark(address);
	}

	/**
	 * @param address
	 * @return
	 * @see emulator.assembler.DataDescriptionTable#getDescriptionForAddress(int)
	 */
	public DataDescription getDescriptionForAddress(int address) {
		return description_table.getDescriptionForAddress(address);
	}

	/**
	 * @param index
	 * @return
	 * @see emulator.assembler.DataDescriptionTable#getDescriptionForIndex(int)
	 */
	public DataDescription getDescriptionForIndex(int index) {
		return description_table.getDescriptionForIndex(index);
	}

	/**
	 * @param address
	 * @return
	 * @see emulator.assembler.DataDescriptionTable#getIndexForAddress(int)
	 */
	public int getIndexForAddress(int address) {
		return description_table.getIndexForAddress(address);
	}

	public void analyzeOneInstruction(int address) {
		Instruction instruction = assembler.getInstruction(memory, address);
		description_table.insertInstruction(address, instruction);
		description_table.reindex();
	}

	public int getAddressForIndex(int line) {
		return description_table.getDescriptionForIndex(line).getAddress();
	}

	public int findLabel(String search_text)
	{
		return assembler.getSymbols().findLabel(search_text);
	}
}